package manager

import (
	"pq/dto"
	"pq/models"
	"pq/repository"
)
var dto2Entity = map[string] func(userDto dto.UserDto,user models.PqUser) models.PqUser{
	"id" : func(userDto dto.UserDto,user models.PqUser) models.PqUser {
		user.Id = userDto.Fields["id"].(int)
		return user
	},
	"category" :  func(userDto dto.UserDto,user models.PqUser) models.PqUser {
		user.PqTestCategoryId = userDto.Fields["category"].(int)
		return user
	},
}
type UserManager struct{

}
//todo
//应该返回一个dto
//问题:domain返回的错误不需要进行判断输出?
func (um *UserManager)UpdateCategory(uid int,cid int)(int64,error){
	slave := models.GetSlaveDb()
	userRepository := repository.UserRepository{}
	userEntity := models.PqUser{Id:uid}
	_ = slave.Read(&userEntity)
	userEntity.PqTestCategoryId = cid
	return userRepository.SaveUser(userEntity)
}

func (um *UserManager)UpdateArea(uid int,areaCode string)(int64,error){
	slave := models.GetSlaveDb()
	userRepository := repository.UserRepository{}
	userEntity := models.PqUser{Id:uid}
	_ = slave.Read(&userEntity)
	userEntity.AreaCode = areaCode
	return userRepository.SaveUser(userEntity)
}

func (um *UserManager) GetCategory(uid int) models.PqTestCategory {
	slave := models.GetSlaveDb()
	var user models.PqUser
	_ = slave.QueryTable("pq_user").Filter("id", uid).One(&user)
	var cate models.PqTestCategory
	_ = slave.QueryTable("pq_test_category").Filter("id",user.PqTestCategoryId).One(&cate)
	return cate
}